import 'dart:io';

import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:untitled1/app/utils/utilDialog.dart';

/// 通用权限工具类（带弹窗提示）
class utilPermission {




  /// 显示权限拒绝弹窗
  static Future<void> _showDeniedDialog({
    required BuildContext context,
    required String title,
    String message = "该权限已被拒绝，请前往设置中开启",
  }) async {
    await showDialog(
      context: context,
      builder:
          (_) => AlertDialog(
            title: Text(title),
            content: Text(message),
            actions: [
              TextButton(
                child: Text("取消"),
                onPressed: () => Navigator.of(context).pop(),
              ),
              TextButton(
                child: Text("去设置"),
                onPressed: () {
                  openAppSettings(); // 打开系统设置
                  Navigator.of(context).pop();
                },
              ),
            ],
          ),
    );
  }

  /// 相机权限
   static Future<bool> requestCameraPermission(BuildContext context) async {
    var status = await Permission.camera.status;
    if (status.isGranted) return true;

    var result = await Permission.camera.request();
    if (result.isGranted) return true;

    // 拒绝或永久拒绝都提示
    await _showDeniedDialog(context: context, title: "相机权限");
    return false;
  }
  //使用  组件中调用
  static requestCameraPermissionUI(BuildContext context) async {
    bool granted = await requestCameraPermission(context);
    if (granted) {
      print("✅ 已授予相机权限");
    } else {
      print("❌ 相机权限被拒绝");
    }
  }

  /// 存储权限（安卓）
  static Future<bool> requestStoragePermission(BuildContext context) async {
    if (Platform.isIOS) return true;

    var status = await Permission.storage.status;
    if (status.isGranted) return true;

    var result = await Permission.storage.request();
    if (result.isGranted) return true;

    await _showDeniedDialog(context: context, title: "存储权限");
    return false;
  }

  //组件中调用
  static requestStoragePermissionUI(BuildContext context) async {
    bool granted = await requestStoragePermission(context);
    if (granted) {
      print("✅ 已授予读写权限");
    } else {
      print("❌ 读写权限被拒绝");
    }
  }

  /// 麦克风权限
  static Future<bool> requestMicrophonePermission(BuildContext context) async {
    var status = await Permission.microphone.status;
    if (status.isGranted) return true;

    var result = await Permission.microphone.request();
    if (result.isGranted) return true;

    await _showDeniedDialog(context: context, title: "麦克风权限");
    return false;
  }

  /// 位置信息权限
  static Future<bool> requestLocationPermission(BuildContext context) async {
    var status = await Permission.locationWhenInUse.status;
    if (status.isGranted) return true;

    var result = await Permission.locationWhenInUse.request();
    if (result.isGranted) return true;

    await _showDeniedDialog(context: context, title: "位置信息权限");
    return false;
  }

  /// 通讯录权限
  static Future<bool> requestContactsPermission(BuildContext context) async {
    var status = await Permission.contacts.status;
    if (status.isGranted) return true;

    var result = await Permission.contacts.request();
    if (result.isGranted) return true;

    await _showDeniedDialog(context: context, title: "通讯录权限");
    return false;
  }
}

//使用：
// static requestStoragePermissionUI(BuildContext context) async {
//   bool granted = await requestStoragePermission2(context);
//   if (granted) {
//     print("✅ 已授予读写权限");
//   } else {
//     print("❌ 读写权限被拒绝");
//   }
// }


//ElevatedButton(
//onPressed: () async {
//bool granted = await PermissionUtil.requestCameraPermission(context);
//if (granted) {
//print("✅ 已授予相机权限");
//} else {
//print("❌ 相机权限被拒绝");
//}
//},
//child: Text("申请相机权限"),
//),
